// wiivie.cpp

// Revision 19 (r19).
// Created by James Madley on 31/1/09.

// Wiivie. Convert video files to Wii compatible video.
// Copyright (C) 2009 James David Madley

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


#include <iostream>
#include "opSys.h"

#if defined (__linux) || (linux)
	#include <cstring>
#endif

using namespace std;

void masterMenu(string encoder_masterMenu, const char *input_masterMenu, const char *output_masterMenu, string dimensions_masterMenu);
void encoderMenu(char encoderMenu_Cross[2]);
void dimensionsMenu(char dimensionsMenu_Cross[7]);
void custom_dimensionsMenu(string width_custom_dimensionsMenu, string height_custom_dimensionsMenu);
string sleepFunc_vars();


int main()
{
	const char *input_masterMenuItem;
	const char *output_masterMenuItem;
	
	string encoder_masterMenuItem = "Not set";
	input_masterMenuItem = "No input selected";
	output_masterMenuItem = "No output selected";
	string dimensions_masterMenuItem = "Default Picture Dimensions";
	string dimensions_masterMenuItemTemp;
	
	char menuChoice;
	char encoderChoice;
	char dimensionsChoice;
	char custom_dimensionsChoice;
	
	string encoder;
	string input = "";
	string output = "";
	string dimensions;
	string dimensionsTemp;
	string width = "Not Defined";
	string height = "Not Defined";
	string convertString;
	
	char encoder_Cross[2] = {' ',' '};
	char dimensions_Cross[7] = {'X',' ',' ',' ',' ',' ',' '};
	
	string sleepNum = sleepFunc_vars();
	
	clearFunc();

	while(menuChoice != 6) 
	{
		masterMenu(encoder_masterMenuItem, input_masterMenuItem, output_masterMenuItem, dimensions_masterMenuItem);
		cin >> menuChoice;
		
		switch(menuChoice)
		{
			case '1':
			{
				clearFunc();
				encoderMenu(encoder_Cross);
				cin >> encoderChoice;
				switch(encoderChoice)
				{
					case '1':
					{
						encoder_Cross[0] = 'X';
						encoder_Cross[1] = ' ';
						encoder_masterMenuItem = "MEncoder";
						clearFunc();
						cout << "MEncoder: ";
						cin.get();
						getline(cin, encoder);
						break;
					}
					case '2':
					{
						encoder_Cross[0] = ' ';
						encoder_Cross[1] = 'X';
						encoder_masterMenuItem = "FFmpeg";
						clearFunc();
						cout << "FFmpeg: ";
						cin.get();
						getline(cin, encoder);
						break;
					}
					default:
					{
						break;
					}
				}
				clearFunc();
				break;
			}
			case '2': 
			{
				clearFunc();
				cout << "Input: ";
				cin.get();
				getline(cin, input);
				
				input_masterMenuItem = input.c_str();
				const char *a = strrchr(input_masterMenuItem, '/');
				
				if ( operatingSystem == "Windows" )
				{
					const char *a = strrchr(input_masterMenuItem, '\\');
				}
				
				if ( a != false )
				{
					a = a + 1;
				}
				else
				{
					a = input_masterMenuItem;
				}
				
				input_masterMenuItem = a;
				
				clearFunc();
				break;
			}
			case '3':
			{
				clearFunc();
				cout << "Output: ";
				cin.get();
				getline(cin, output);
				
				output_masterMenuItem = output.c_str();
				const char *b = strrchr(output_masterMenuItem, '/');
				
				if ( operatingSystem == "Windows" )
				{
					const char *b = strrchr(output_masterMenuItem, '\\');
				}
				
				if ( b != false )
				{
					b = b + 1;
				}
				else
				{
					b = output_masterMenuItem;
				}
				
				output_masterMenuItem = b;
				
				clearFunc();
				break;
			}
			case '4':
			{
				clearFunc();
				dimensionsMenu(dimensions_Cross);
				cin >> dimensionsChoice;
				switch(dimensionsChoice)
				{
					case '1': 
					{
						dimensions_Cross[0] = 'X';
						dimensions_Cross[1] = ' ';
						dimensions_Cross[2] = ' ';
						dimensions_Cross[3] = ' ';
						dimensions_Cross[4] = ' ';
						dimensions_Cross[5] = ' ';
						dimensions_Cross[6] = ' ';
						dimensions_masterMenuItem = "Default Picture Dimensions";
						dimensions = "";
						break;
					}
					case '2':
					{
						dimensions_Cross[0] = ' ';
						dimensions_Cross[1] = 'X';
						dimensions_Cross[2] = ' ';
						dimensions_Cross[3] = ' ';
						dimensions_Cross[4] = ' ';
						dimensions_Cross[5] = ' ';
						dimensions_Cross[6] = ' ';
						dimensions_masterMenuItem = "Tiny Picture Dimensions";
						dimensions = "-vf scale -zoom -xy 160";
						break;
					}
					case '3':
					{
						dimensions_Cross[0] = ' ';
						dimensions_Cross[1] = ' ';
						dimensions_Cross[2] = 'X';
						dimensions_Cross[3] = ' ';
						dimensions_Cross[4] = ' ';
						dimensions_Cross[5] = ' ';
						dimensions_Cross[6] = ' ';
						dimensions_masterMenuItem = "Small Picture Dimensions";
						dimensions = "-vf scale -zoom -xy 320";
						break;
					}
					case '4':
					{
						dimensions_Cross[0] = ' ';
						dimensions_Cross[1] = ' ';
						dimensions_Cross[2] = ' ';
						dimensions_Cross[3] = 'X';
						dimensions_Cross[4] = ' ';
						dimensions_Cross[5] = ' ';
						dimensions_Cross[6] = ' ';
						dimensions_masterMenuItem = "Normal Picture Dimensions";
						dimensions = "-vf scale -zoom -xy 640";
						break;
					}
					case '5':
					{
						dimensions_Cross[0] = ' ';
						dimensions_Cross[1] = ' ';
						dimensions_Cross[2] = ' ';
						dimensions_Cross[3] = ' ';
						dimensions_Cross[4] = 'X';
						dimensions_Cross[5] = ' ';
						dimensions_Cross[6] = ' ';
						dimensions_masterMenuItem = "Large Picture Dimensions";
						dimensions = "-vf scale -zoom -xy 720";
						break;
					}
					case '6':
					{
						dimensions_Cross[0] = ' ';
						dimensions_Cross[1] = ' ';
						dimensions_Cross[2] = ' ';
						dimensions_Cross[3] = ' ';
						dimensions_Cross[4] = ' ';
						dimensions_Cross[5] = 'X';
						dimensions_Cross[6] = ' ';
						dimensions_masterMenuItem = "Huge Picture Dimensions";
						dimensions = "-vf scale -zoom -xy 848";
						break;
					}
					case '7':
					{
						clearFunc();
						
						dimensions_Cross[0] = ' ';
						dimensions_Cross[1] = ' ';
						dimensions_Cross[2] = ' ';
						dimensions_Cross[3] = ' ';
						dimensions_Cross[4] = ' ';
						dimensions_Cross[5] = ' ';
						dimensions_Cross[6] = 'X';
						
						custom_dimensionsChoice = 0;
						
						
						while(custom_dimensionsChoice != '3')
						{
							custom_dimensionsMenu(width, height);
							cin >> custom_dimensionsChoice;
							switch(custom_dimensionsChoice)
							{
								case '1':
								{
									clearFunc();
									cout << "Width: ";
									cin.get();
									getline(cin, width);
								
									dimensions_masterMenuItemTemp = width + "x" + height;
									dimensions_masterMenuItem = dimensions_masterMenuItemTemp.c_str();
								
									dimensionsTemp = "-vf scale=" + width + ":" + height;
									dimensions = dimensionsTemp.c_str();
									
									clearFunc();
								
									break;
								}
								case '2':
								{
									clearFunc();
									cout << "Height: ";
									cin.get();
									getline(cin, height);
								
									dimensions_masterMenuItemTemp = width + "x" + height;
									dimensions_masterMenuItem = dimensions_masterMenuItemTemp.c_str();
								
									dimensionsTemp = "-vf scale=" + width + ":" + height;
									dimensions = dimensionsTemp.c_str();
									
									clearFunc();
								
									break;
								}
								case '3':
								{
									break;
								}
								default:
								{
									clearFunc();
									cout << "\n*YOU DID NOT ENTER A VALID MENU OPTION. PLEASE TRY AGAIN* \n \n";
									sleepFunc(sleepNum);
									clearFunc();
									break;
								}
							}
						}
					}
				}
			}
			case '5':
			{
				convertString = encoder + " " + input + " -ovc lavc -lavcopts vcodec=mjpeg -oac pcm " + dimensions + " -o " + output;
				system(convertString.c_str());
				input = "";
				output = "";
				input_masterMenuItem = "No input selected";
				output_masterMenuItem = "No outout selected";
				clearFunc();
				break;
			}
			case '6':
			{
				return 0;
				break;
			}
			default:
			{
				clearFunc();
				cout << "\n*YOU DID NOT ENTER A VALID MENU OPTION. PLEASE TRY AGAIN* \n \n";
				sleepFunc(sleepNum);
				clearFunc();
				break;
			}
		} 
	}
          
	return 0;
}


void masterMenu(string encoder_masterMenu, const char *input_masterMenu, const char *output_masterMenu, string dimensions_masterMenu)
{
	cout << "=============================================== \n";
	cout << "  Please choose an item on the menu below \n";
	cout << "-----------------------------------------------\n";
	cout << "               (1) Encoder: " << encoder_masterMenu << "\n";
	cout << "               (2) " << input_masterMenu << "\n";
	cout << "               (3) " << output_masterMenu << "\n";
	cout << "               (4) " << dimensions_masterMenu << "\n";
	cout << "               (5) Start encoding! \n";
	cout << "               (6) Quit \n";
	cout << "=============================================== \n";
	cout << "Your Choice: ";
}


void encoderMenu(char encoderMenu_Cross[2])
{
	cout << "=============================================== \n";
	cout << "  Please choose an item on the menu below \n";
	cout << "-----------------------------------------------\n";
	cout << "               [" << encoderMenu_Cross[0] << "] (1) MEncoder \n";
	cout << "               [" << encoderMenu_Cross[1] << "] (2) FFmpeg \n";
	cout << "=============================================== \n";
	cout << "Your Choice: ";
}


void dimensionsMenu(char dimensionsMenu_Cross[7])
{
	cout << "=============================================== \n";
	cout << "  Please choose an item on the menu below \n";
	cout << "-----------------------------------------------\n";
	cout << "               [" << dimensionsMenu_Cross[0] << "] (1) Default \n";
	cout << "               [" << dimensionsMenu_Cross[1] << "] (2) Tiny \n";
	cout << "               [" << dimensionsMenu_Cross[2] << "] (3) Small \n";
	cout << "               [" << dimensionsMenu_Cross[3] << "] (4) Normal \n";
	cout << "               [" << dimensionsMenu_Cross[4] << "] (5) Large \n";
	cout << "               [" << dimensionsMenu_Cross[5] << "] (6) Huge \n";
	cout << "               [" << dimensionsMenu_Cross[6] << "] (7) Custom \n";
	cout << "=============================================== \n";
	cout << "Your Choice: ";
}


void custom_dimensionsMenu(string width_custom_dimensionsMenu, string height_custom_dimensionsMenu)
{
	cout << "=============================================== \n";
	cout << "  Please choose an item on the menu below \n";
	cout << "-----------------------------------------------\n";
	cout << "               (1) Width:  " << width_custom_dimensionsMenu << "\n";
	cout << "               (2) Height: " << height_custom_dimensionsMenu << "\n";
	cout << "               (3) Main Menu \n";
	cout << "=============================================== \n";
	cout << "Your Choice: ";
}


string sleepFunc_vars()
{
	if ( operatingSystem == "UNIX" )
	{
		return "5";
	}
	else if ( operatingSystem == "Windows" )
	{
		return "5000";
	}
	
	return "";
}
